package com.lam.system.service.impl;

import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.dict.constant.DictConstant;
import com.lam.common.components.easyexcel.imp.model.AbstractImportBody;
import com.lam.common.components.easyexcel.imp.model.ImportEntry;
import com.lam.common.components.easyexcel.imp.model.ImportWrapper;
import com.lam.common.components.easyexcel.imp.service.AbstractImportService;
import com.lam.common.components.redis.RedisUtil;
import com.lam.common.constant.SystemConstant;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.ValidateUtil;
import com.lam.common.validate.model.UniqueDesc;
import com.lam.common.validate.model.ValidOperType;
import com.lam.framework.bean.entity.SysTableDict;
import com.lam.system.entity.imp.SysTableDictImp;
import com.lam.system.mapper.SysTableDictMapper;
import com.lam.system.param.SysTableDictParam;
import com.lam.system.service.SysTableDictService;

/**
 * Table字典配置Service业务层处理
 * 
 * @author admin
 * @date 2022-08-01
 */
@Service
public class SysTableDictServiceImpl extends AbstractImportService<SysTableDictImp, SysTableDict> implements SysTableDictService {

    @Autowired
    private SysTableDictMapper sysTableDictMapper;
    
    /**
     * 查询Table字典配置
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysTableDict selectById(String id) {

        return sysTableDictMapper.selectById(id);
    }

    /**
     * 查询Table字典配置列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysTableDict> selectList(SysTableDictParam param) {
    
        QueryWrapper<SysTableDict> queryWrapper = new QueryWrapper<>();
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
        		wr.or().like("table_name", param.getKeywords());
        		wr.or().like("dict_code", param.getKeywords());
        		wr.or().like("value_field", param.getKeywords());
        		wr.or().like("text_field", param.getKeywords());
        	});
        }
        queryWrapper.eq(StringUtils.isNotBlank(param.getDictCode()), "dict_code", param.getDictCode());
        queryWrapper.like(StringUtils.isNotBlank(param.getTableNameKey()), "table_name", param.getTableNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTableName()), "table_name", param.getTableName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getValueField()), "value_field", param.getValueField());
        queryWrapper.eq(StringUtils.isNotBlank(param.getTextField()), "text_field", param.getTextField());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.eq(Objects.nonNull(param.getType()), "type", param.getType());

        return sysTableDictMapper.selectList(queryWrapper);
    }

    /**
     * 新增Table字典配置
     * 
     * @param sysTableDict 
     * @return 
     */
    @Override
//    @CacheEvict(value = SystemConstant.TABLE_DICT_CONFIG, key = "#sysTableDict.dictCode")
    @Transactional
    public int insert(SysTableDict sysTableDict) {
    
        int n = sysTableDictMapper.insert(sysTableDict);
        if(n > 0) {
        	RedisUtil.del(DictConstant.TABLE_DICT_CONFIG+sysTableDict.getDictCode());
        	RedisUtil.removeAll(DictConstant.SYS_TABLE_DICT_PREFIX + sysTableDict.getTableName());
        }
        return n;
    }

    /**
     * 修改Table字典配置
     * 
     * @param sysTableDict 
     * @return 
     */
    @Override
//    @CacheEvict(value = SystemConstant.TABLE_DICT_CONFIG, key = "#sysTableDict.dictCode")
    @Transactional
    public int update(SysTableDict sysTableDict) {
    
        int n = sysTableDictMapper.updateById(sysTableDict);
        if(n > 0) {
        	RedisUtil.del(DictConstant.TABLE_DICT_CONFIG+sysTableDict.getDictCode());
        	RedisUtil.removeAll(DictConstant.SYS_TABLE_DICT_PREFIX + sysTableDict.getTableName());
        }
        return n;
    }

    /**
     * 删除Table字典配置信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	int n = 0;
    	SysTableDict tableDict;
    	for (String id : StringUtils.str2List(idStr)) {
    		tableDict = selectById(id);
    		if(tableDict != null) {
    			n += sysTableDictMapper.deleteById(id);
    			RedisUtil.del(DictConstant.TABLE_DICT_CONFIG+tableDict.getDictCode());
    			RedisUtil.removeAll(DictConstant.SYS_TABLE_DICT_PREFIX + tableDict.getTableName());
    		}
        }
        return n;
    }
    
    @Override
    @Transactional
    public void importData(ImportWrapper<SysTableDictImp, SysTableDict> wrapper) {
        
    	UniqueDesc uniqueDesc = ValidateUtil.selectUniqueDesc(SystemConstant.UNIQUE_TABLE_DICT_CODE);
    	importData(wrapper, new AbstractImportBody<SysTableDictImp, SysTableDict>() {
			@Override
			public SysTableDict selectRecord(ImportEntry<SysTableDictImp, SysTableDict> entry) {
				return ValidateUtil.selectUniqueData(entry.getData(), ValidOperType.ADD, uniqueDesc, SysTableDict.class);
			}
			@Override
			public int insertData(SysTableDict data) {
				return insert(data);
			}
			@Override
			public int updateData(SysTableDict data) {
				return update(data);
			}
			@Override
			public String notUniqueMsg(ImportEntry<SysTableDictImp, SysTableDict> entry) {
				return MessageFormat.format("字典编码【{0}】已存在", entry.getData().getDictCode());
			}
    	});
    }

}
